<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/6
 * Time: 18:30
 */

namespace XiaoZhuBundle\Twig;


class XZExtension extends \Twig_Extension
{
    private $REQUEST;
    private $DOCTRINE;
    private $CONFIG;

    public function __construct($REQUEST,$DOCTRINE,$CONFIG)
    {
        $this->REQUEST = $REQUEST->getCurrentRequest();
        $this->DOCTRINE = $DOCTRINE;
        $this->CONFIG = $CONFIG;
    }
    public function getGlobals()
    {
        $config = $this->CONFIG;
        $info = array();
        $site = array('title'=>$config->get("site_name"), 'description'=>$config->get("site_description"), 'keywords'=>$config->get("site_keywords"), 'phone'=>$config->get("site_phone"));
        $info['site'] = $site;
        //购物车
        $carts = $this->getCart();
        $good_num = $carts["num"];
        $good_money = $carts["total"];
        $good = $carts["lists"];
        //用户信息
        $userInfo = $this->getSessionUserInfo();
        $tmp = array(
            'username'=>$userInfo["username"], 'logout'=>$userInfo["logout"],
            'count'=>$good_num, 'money'=>$good_money, 'good'=>$good);
        $info['tmp'] = $tmp;

        ////////////////////////////////////////////////////////////
        //分类信息
        $cat_cat1_attr1 = array('银镶钻','铂金镶钻','钯金镶钻','珍珠','施华洛世奇元素','天然宝石','人造水晶','人工宝石','K金镶钻');
        $cat_cat1_attr2 = array('貔貅','观音','花草','生肖','动物','关公','佛','人物');
        $cat_cat1_attr3 = array('100~150','150~200','200~250','250~300','300~350','350以上');
        $cat_cat1_bands = array('bundles/xiaozhu//temp/brands/next.jpg','bundles/xiaozhu//temp/brands/next.jpg','bundles/xiaozhu//temp/brands/next.jpg','bundles/xiaozhu//temp/brands/next.jpg','bundles/xiaozhu//temp/brands/next.jpg');
        $cat['cat1']=array('attr1'=>$cat_cat1_attr1, 'attr2'=>$cat_cat1_attr2, 'attr3'=>$cat_cat1_attr3, 'bands'=>$cat_cat1_bands);

        $cat_cat2_attr1 = array('婚庆','生肖','圆形/水滴','花草','心形','字母','卡通','扁圆');
        $cat_cat2_attr2 = array('耳钉','耳环','耳线','耳夹','其它');
        $cat['cat2']=array('attr1'=>$cat_cat2_attr1, 'attr2'=>$cat_cat2_attr2);

        $cat_cat3_attr1 = array('黑色','白色','红色','紫色','绿色','橙色','驼色','花色','灰色','粉色','玫瑰红');
        $cat_cat3_attr2 = array('发箍/发圈','横夹','边夹','抓夹','发绳','插梳','发饰套装','发梳','竖夹/香蕉夹','皇冠','八字夹','盘发棒','顶夹','簪子');
        $cat['cat3']=array('attr1'=>$cat_cat3_attr1,'attr2'=>$cat_cat3_attr2);

        $cat_cat4_attr1=array('情人节','生日','纪念日','新年礼物','七夕节');
        $cat_cat4_attr2 = array('升学礼','升迁礼','聚会礼','送行礼');
        $cat['cat4'] = array('attr1'=>$cat_cat4_attr1, 'attr2'=>$cat_cat4_attr2);

        $cat_cat5_attr1=array('施华洛世奇','T400','千寻海风','JOLEE','旭平首饰','简丹');
        $cat_cat5_attr2=array('陈欣','依泰连娜','烟雨江南','special','萌趣趣','瑞诺');
        $cat_cat5_attr3=array('美丽公主','银千惠','倾爱','凤凰涅槃','城市玉光','AGATHA');
        $cat['cat5'] = array('attr1'=>$cat_cat5_attr1, 'attr2'=>$cat_cat5_attr2, 'attr3'=>$cat_cat5_attr3);

        $info['cat']=$cat;
        $info['foot_pages'] = $this->getAllPage();
        $info['foot_news'] = $this->getNews();
        return $info;
    }

    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return "xz_extension";
    }

    public function getSessionUserInfo()
    {
        if (!$this->REQUEST->hasSession()) {
            $this->REQUEST->setSession(array("startTime",time()));
        }
        $session =  $this->REQUEST->getSession();
        if ($session->has("userInfo")) {
            $info = $session->get("userInfo");
        } else {
            $info = array();
        }

        if (!isset($info["logout"])) {
            $info = array(
                "username"  => "游客",
                "logout"    => TRUE,
            );
            $this->setSessionUserInfo($info);

        }
        return $info;
    }
    public function setSessionUserInfo($data = array())
    {
        if (is_array($data) && !empty($data)) {
            $this->REQUEST
                ->getSession()->set( "userInfo" , $data );
        }
    }

    public function getAllPage()
    {
        $rep = $this->DOCTRINE->getManager()->getRepository("AdminsBundle:Page");
        return $rep->find4Index();
    }

    public function getNews()
    {
        $rep = $this->DOCTRINE->getManager()->getRepository("AdminsBundle:News");
        return $rep->find4Index();
    }

    public function getCart()
    {
        if (!$product = $this->REQUEST->cookies->get("cart")) {$product=array();}
        $lists = array();
        $total = 0;
        $count= 0;
        if (is_array($product) && !empty($product)) {
            foreach ($product as $pid => $info) {
                $num = $info["num"];
                $rep = $this->DOCTRINE->getManager()->getRepository("AdminsBundle:Product");
                $productBasic = $rep->findOneBy(array("id"=>$pid));
                if (!$productBasic) {
                    continue;
                }
                $rep = $this->DOCTRINE->getManager()->getRepository("AdminsBundle:ProductShow");
                $productShow = $rep->findOneBy(array("id"=>$pid));
                $lists[] = array(
                    "basic"   => $productBasic ,
                    "show"   => $productShow,
                    "num"   => $num,
                    "total"     => $num * $productBasic->getPrice()
                );
                $total +=  $num * $productBasic->getPrice();
                $count += $num;
            }
        }
        return array("lists"=>$lists,"total"=> $total,"num"=>$count);
    }
}